<?php

namespace App\Http\Controllers\Sample;

use App\Helpers\Code;
use App\Models\Sample\SampleInoutRecord;
use App\Models\Sample\SampleInoutRecordDetail;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class InoutController extends Controller
{

    /**
     * @author lcy
     */
    public function __construct()
    {
        $this->_validators();
        $this->model = new SampleInoutRecord();
    }

    /**
     * 添加发送单
     * @param Request $request
     * @return array
     */
    public function addOutForm(Request $request)
    {
        try {
            $data = $request->only(['description', 'enter_date', 'checked_group', 'checked_by', 'inout_date', 'express_company', 'express_number', 'project', 'salesman']);
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addOutForm($data);
            $this->checked($model, [
                'null' => 'sample.outForm.user_non_exist',
                Code::INSIDE_ERROR => 'sample.outForm.dependence_non_exist',
            ]);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑发送单
     * @param Request $request $id
     * @return array
     */
    public function editOutForm(Request $request, $id)
    {
        try {
            $data = $request->only(['description', 'enter_date', 'checked_group', 'checked_by', 'inout_date', 'express_company', 'express_number', 'project', 'salesman']);
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $detailArr = json_decode($request->input('samples'), true);
            if (!is_array($detailArr) || count($detailArr) == 0) {
                return $this->error('common.data_format_error');
            }

            $model = $this->model->editOutForm($data, array_unique($detailArr), $id);
            $this->checked($model, [
                'null' => 'common.get_failure',
                Code::INSIDE_ERROR => 'sample.outForm.user_non_exist',
                Code::INSIDE_ERROR_1 => 'sample.outForm.dependence_non_exist',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取指定发送单
     * @param $id
     */
    public function getOutForm($id)
    {
        try {
            $model = $this->model->getOutForm($id);
            $this->checked($model, ['null' => 'common.get_failure']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取发送单列表
     * @param Request $request
     */
    public function getOutFormList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);
            $limit = (int)$request->input('limit');
            $sorter = $this->handleSort($request, $this->model->sorters);
            $defaultSorter = $request->input('sorter') ?? 'desc';
            return preprocess($this->model->getOutFormList($limit, $sorter, $search, $defaultSorter));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 发送单提交审核
     * @param $id
     */
    public function submitOutForm($id)
    {
        try {
            $model = $this->model->submitOutForm($id);
            $this->checked($model, ['null' => 'sample.outForm.outForm_non_exist']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 发送单审核通过
     * @param Request $request $id
     */
    public function approveOutForm(Request $request, $id)
    {
        try {
            $data = $request->only(['state']);
            $this->rules = ['state' => 'bail|required|in:2,3'];
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->approveOutForm($data, $id);
            $this->checked($model, ['null' => 'sample.outForm.outForm_non_exist']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取指定发送单包含样本
     * @param Request $request $id
     */
    public function getOutFormSampleList(Request $request, $id)
    {
        try {
            $limit = (int)$request->input('limit');
            $this->model = new SampleInoutRecordDetail();
            return preprocess($this->model->getOutFormSampleList($limit, $id));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 添加接收单
     * @param Request $request
     */
    public function addInForm(Request $request)
    {
        try {
            $data = $request->only(['description', 'enter_date', 'checked_group', 'checked_by', 'inout_date']);
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $detailArr = json_decode($request->input('samples'), true);
            if (!is_array($detailArr) || count($detailArr) == 0) {
                return $this->error('common.data_format_error');
            }

            $model = $this->model->addInForm($data, array_unique($detailArr));
            $this->checked($model, [
                'null' => 'sample.outForm.user_non_exist',
                Code::INSIDE_ERROR_1 => 'sample.outForm.sample_non_exist',
                Code::INSIDE_ERROR_2 => 'sample.outForm.detail_exist',
            ]);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑接收单
     * @param Request $request
     */
    public function editInForm(Request $request, $id)
    {
        try {
            $data = $request->only(['description', 'enter_date', 'checked_group', 'checked_by', 'inout_date']);
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $detailArr = json_decode($request->input('samples'), true);
            if (!is_array($detailArr) || count($detailArr) == 0) {
                return $this->error('common.data_format_error');
            }

            $model = $this->model->editInForm($data, array_unique($detailArr), $id);
            $this->checked($model, [
                'null' => 'common.get_failure',
                Code::INSIDE_ERROR => 'sample.outForm.user_non_exist',
                Code::INSIDE_ERROR_1 => 'sample.outForm.sample_non_exist',
            ]);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 接收单提交审核
     * @param $id
     */
    public function submitInForm($id)
    {
        try {
            $model = $this->model->submitInForm($id);
            $this->checked($model, ['null' => 'sample.inForm.inForm_non_exist']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 接收单审核通过
     * @param Request $request $id
     */
    public function approveInForm(Request $request, $id)
    {
        try {
            $data = $request->only(['state']);
            $this->rules = ['state' => 'bail|required|in:2,3'];
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->approveInForm($data, $id);
            $this->checked($model, ['null' => 'sample.inForm.inForm_non_exist']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取接收单列表
     * @param Request $request
     */
    public function getInForm($id)
    {
        try {
            $model = $this->model->getInForm($id);
            $this->checked($model, ['null' => 'common.get_failure']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取接收单列表
     * @param Request $request
     */
    public function getInFormList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);
            $limit = (int)$request->input('limit');
            $sorter = $this->handleSort($request, $this->model->sorters);
            $defaultSorter = $request->input('sorter') ?? 'desc';
            return preprocess($this->model->getInFormList($limit, $sorter, $search, $defaultSorter));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取指定接收单包含的样本
     * @param Request $request $id
     */
    public function getInFormSampleList(Request $request, $id)
    {
        try {
            $limit = (int)$request->input('limit');
            $this->model = new SampleInoutRecordDetail();
            return preprocess($this->model->getOutFormSampleList($limit, $id));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'description' => 'max:200',
            'enter_date' => 'nullable|date',
            'checked_group' => 'bail|required|numeric',
            'checked_by' => 'nullable|numeric',
            'submitted_by' => 'nullable|numeric',
            'submit_time' => 'nullable|date',
            'inout_date' => 'nullable|date',
            'express_company' => 'max:200',
            'express_number' => 'max:200',
            'project' => 'nullable|numeric',
            'salesman' => 'nullable|numeric',
        ];

        $this->messages = [
            'description.max' => trans('sample.outForm.description_max'),
            'enter_date.date' => trans('sample.outForm.enter_date'),
            'checked_group.required' => trans('sample.outForm.checked_group_required'),
            'checked_group.numeric' => trans('sample.outForm.checked_group_numeric'),
            'checked_by.numeric' => trans('sample.outForm.checked_by_numeric'),
            'submitted_by.numeric' => trans('sample.outForm.submitted_by_numeric'),
            'submit_time.date' => trans('sample.outForm.submit_time_date'),
            'inout_date.date' => trans('sample.outForm.inout_date'),
            'express_company.max' => trans('sample.outForm.express_company_max'),
            'express_number.max' => trans('sample.outForm.express_number_max'),
            'project.numeric' => trans('sample.outForm.project_numeric'),
            'salesman.numeric' => trans('sample.outForm.salesman_numeric'),
            'state.required' => trans('sample.outForm.state_required'),
            'state.in' => trans('sample.outForm.state_in'),
        ];
    }
}
